home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Programmer Disk
/
The Programmer Disk (Microforum).iso
/
xpro
/
vbdos
/
pro9
/
valrepl3.bas
< prev
Wrap
BASIC Source File
|
1993-03-15
|
3KB
|
94 lines
DECLARE FUNCTION ValReplacement@ (TS$)
TYPE MyCurrency
MyNum AS CURRENCY
END TYPE
TYPE MyLong
MyNum AS LONG
MyFil AS LONG
END TYPE
CLS
INPUT "Enter Test Numeric String "; S$
PRINT "ValR "; ValReplacement(S$)
PRINT "VAL "; VAL(S$)
PRINT "Benchmarking"
MarkTime& = TIMER
WHILE MarkTime& = TIMER: WEND
FOR J% = 1 TO 32000
NEXT
Time3& = TIMER - MarkTime& - 1
MarkTime& = TIMER
WHILE MarkTime& = TIMER: WEND
FOR J% = 1 TO 32000
Dmy@ = ValReplacement(S$)
NEXT
Time1& = TIMER - MarkTime& - 1 - Time3&
MarkTime& = TIMER
WHILE MarkTime& = TIMER: WEND
FOR J% = 1 TO 32000
Dmy@ = VAL(S$)
NEXT
Time2& = TIMER - MarkTime& - 1 - Time3&
PRINT Time1&; " Seconds For ValReplacement"
PRINT Time2&; " Seconds For VAL"
PRINT "ValReplacement:VAL Ratio ";
PRINT USING "###.##"; Time1& / Time2&
'Something fun to do during the blizzard of '93
'Replaces the VAL function when you want to eliminate floating point
'but need to VAL some strings
'
'Legitimate values are - 2,147,483,647.9999 to 2,147,483,647.9999
'Benchmark to VAL is dependent on String Length AND if fractions occur
'
'Courtesy of Brian McMahon 75430,717
'
FUNCTION ValReplacement@ (TS$)
STATIC MyCurr AS MyCurrency, MyL AS MyLong, S$, LenS%, Negate%, Skip%, Scale%, NewVal&, SaveIt@, J%
S$ = LTRIM$(RTRIM$(TS$))'get rid of any space on either end
LenS% = LEN(S$)
IF LenS% = 0 THEN
ValReplacement = 0
EXIT FUNCTION
END IF
IF ASC(S$) = ASC("-") THEN Negate% = 2 ELSE Negate% = 1
Skip% = INSTR(S$, ".")
IF Skip% THEN
Scale% = LenS% - Skip%
IF Scale% > 4 THEN LenS% = LenS% - (Scale% - 4): Scale% = 0 ELSE Scale% = 4 - Scale%
Skip% = Skip% - 1
ELSE
Skip% = LenS%
END IF
NewVal& = 0
FOR J% = Negate% TO Skip%
NewVal& = NewVal& * 10 + ASC(MID$(S$, J%, 1)) - ASC("0")
NEXT
IF Skip% <> LenS% THEN 'a fraction
SaveIt@ = NewVal& 'let basic do the type casting on the first part
NewVal& = 0
Skip% = Skip% + 2
FOR J% = Skip% TO LenS%
NewVal& = NewVal& * 10 + ASC(MID$(S$, J%, 1)) - ASC("0")
NEXT
FOR J% = 1 TO Scale% 'scale the fraction
NewVal& = NewVal& * 10
NEXT
MyL.MyNum = NewVal& ' type cast to a currency fraction
LSET MyCurr = MyL ' via lset
IF Negate% <> 2 THEN ValReplacement = SaveIt@ + MyCurr.MyNum ELSE ValReplacement = -(SaveIt@ + MyCurr.MyNum)
ELSE
IF Negate% <> 2 THEN ValReplacement = NewVal& ELSE ValReplacement = -NewVal&
END IF
END FUNCTION